package ngames.com.sushitv.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.action.SearchAction;
import ngames.com.sushitv.entity.SearchPrompt;
import ngames.com.sushitv.interfaces.ChangFragment;
import ngames.com.sushitv.view.ClearEditText;

/**
 * Created by edmond on 16-7-26.
 */
public class SearchFragment extends Fragment {
    private View view;
    private Fragment firstFragment, historyFragment, promptFragment, resultFragment;
    private ClearEditText clearEditText;
    private Button cancelBtn;
    private SearchAction searchAction;
    private ChangFragment changef = new ChangFragment() {
        @Override
        public void ChangeFragment(int i) {
            mChangeFragment(i);
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search, container, false);
        AppApplication.setSearchFragmentView(view);
        searchAction = new SearchAction(getActivity(), view, changef);
        searchAction.onCreate();
        initView();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        mChangeFragment(0);
        Log.e("fragment","I'm here");
        super.onHiddenChanged(hidden);
    }

    private void initView() {
        mChangeFragment(0);
        clearEditText = (ClearEditText) view.findViewById(R.id.clearEdit_search);
        cancelBtn = (Button) view.findViewById(R.id.btn_searchFragment_cancel);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearEditText.setText("");
                cancelBtn.setVisibility(View.GONE);
                mChangeFragment(0);
            }
        });
        cancelBtn.setVisibility(View.GONE);
        /*clearEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getAppApplication(),"ok",Toast.LENGTH_SHORT).show();
            }
        });*/

        clearEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    cancelBtn.setVisibility(View.VISIBLE);
                    mChangeFragment(1);
                    /*fragment=new SearchFragment();
                    fragment.ChangeFragment(1);*/
                }
            }
        });
        clearEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //Toast.makeText(getAppApplication(),"ok1",Toast.LENGTH_SHORT).show();
                //filterData(charSequence.toString());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //Toast.makeText(getAppApplication(),"ok2",Toast.LENGTH_SHORT).show();
                filterData(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //Toast.makeText(getAppApplication(),"ok3",Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 过滤字符
     */
    private void filterData(String s) {
        cancelBtn.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(s)) {
            mChangeFragment(1);
        } else {
            searchAction.searchPrompt(s);
        }
    }

    public void mChangeFragment(int i) {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        switch (i) {
            case 0:
                if (firstFragment == null) {
                    firstFragment = new SearchFirstFragment();
                    ft.add(R.id.linear_search_bottom, firstFragment);
                } else {
                    ft.show(firstFragment);
                }

                break;
            case 1:
                if (historyFragment == null) {
                    historyFragment = new SearchHistoryFragment();
                    ft.add(R.id.linear_search_bottom, historyFragment);
                } else {
                    ft.show(historyFragment);
                }
                break;
            case 2:
                if (promptFragment == null) {
                    promptFragment = new SearchPromptFragment();
                    ft.add(R.id.linear_search_bottom, promptFragment);
                } else {
                    ft.show(promptFragment);
                }
                break;
            case 3:
                if (resultFragment == null) {
                    resultFragment = new SearchResultFragment();
                    ft.add(R.id.linear_search_bottom, resultFragment);
                } else {
                    ft.show(resultFragment);
                }
                break;
        }
        ft.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (firstFragment != null) {
            ft.hide(firstFragment);
        }
        if (historyFragment != null) {
            ft.hide(historyFragment);
        }
        if (promptFragment != null) {
            //ft.hide(promptFragment);
            ft.remove(promptFragment);
            promptFragment = null;
        }
        if (resultFragment != null) {
            ft.remove(resultFragment);
            resultFragment = null;
        }
    }
}
